package com.youbug.mall.ware_wms.service.impl;

import com.youbug.common.to.PmsSkuInfoTo;
import com.youbug.mall.ware_wms.entity.WmsPurchaseDetail;
import com.youbug.mall.ware_wms.entity.WmsWareInfo;
import com.youbug.mall.ware_wms.feign.IWmsPmsFeignService;
import com.youbug.mall.ware_wms.mapper.WmsPurchaseDetailMapper;
import com.youbug.mall.ware_wms.service.IWmsPurchaseDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youbug.mall.ware_wms.service.IWmsWareInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author youbug
 * @since 2024-05-05
 */
@Service
public class WmsPurchaseDetailServiceImpl extends ServiceImpl<WmsPurchaseDetailMapper, WmsPurchaseDetail> implements IWmsPurchaseDetailService {

        @Autowired
        private IWmsPmsFeignService pmsFeignService;

        @Autowired
        private IWmsWareInfoService wareInfoService;

        @Override
        public IPage<WmsPurchaseDetail> getWmsPurchaseDetailPage(QueryVo<WmsPurchaseDetail> queryVo) {

        Page<WmsPurchaseDetail> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<WmsPurchaseDetail> wrapper = new QueryWrapper<>();

        WmsPurchaseDetail wmsPurchaseDetail = queryVo.getPayload();

        if(!ObjectUtils.isEmpty(wmsPurchaseDetail)){
                if(wmsPurchaseDetail.getWareId()!= null){
                wrapper.eq("ware_id", wmsPurchaseDetail.getWareId());
                }
                if(wmsPurchaseDetail.getSkuId()!= null){
                wrapper.eq("sku_id", wmsPurchaseDetail.getSkuId());
                }
                if(wmsPurchaseDetail.getStatus()!= null){
                wrapper.eq("status", wmsPurchaseDetail.getStatus());
                }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
        wrapper.orderByDesc("create_time");

        Page<WmsPurchaseDetail> page = this.page(paramPage, wrapper);
            List<WmsPurchaseDetail> records = page.getRecords();
            setSkuName(records);
            setWareName(records);
            return page;
        }

    private void setSkuName(List<WmsPurchaseDetail> records) {
        if(!CollectionUtils.isEmpty(records)){
            List<Long> ids = records.stream().map(WmsPurchaseDetail::getSkuId).collect(Collectors.toList());
            Map<Long, PmsSkuInfoTo> infoToMap = pmsFeignService.getSkuInfoByIds(ids);
            records.forEach(e->{
                PmsSkuInfoTo skuInfoTo = infoToMap.get(e.getSkuId());
                if(skuInfoTo!=null){
                    e.setSkuNameTemp(skuInfoTo.getSkuName());
                }
            });
        }
    }
    private void setWareName(List<WmsPurchaseDetail> records) {
        if(!CollectionUtils.isEmpty(records)){
            Set<Long> wareIds = records.stream().map(WmsPurchaseDetail::getWareId).collect(Collectors.toSet());
            Map<Long, String> stringMap = wareInfoService.listByIds(wareIds).stream().collect(Collectors.toMap(WmsWareInfo::getId, e -> e.getAddress() + e.getName()));
            records.forEach(e->{
                String wareName = stringMap.get(e.getWareId());
                e.setWareNameTemp(wareName);
            });
        }
    }

    /* @Transactional(transactionManager = "transactionManagerXXX", rollbackFor = Exception.class)
     public boolean saveBatch(Collection<WmsPurchaseDetail> entityList) {
     return saveBatch(entityList, DEFAULT_BATCH_SIZE);
     }
     */
        @Override
        @Transactional(transactionManager = "transactionManagerWms", rollbackFor = Exception.class)
        public boolean updateBatchById(Collection<WmsPurchaseDetail> entityList) {
        return updateBatchById(entityList, DEFAULT_BATCH_SIZE);
        }

        @Override
        public List<WmsPurchaseDetail> getPurchaseDetails(Long id) {
            List<WmsPurchaseDetail> detailList = this.list(new QueryWrapper<WmsPurchaseDetail>().eq("purchase_id", id));
            setSkuName(detailList);
            setWareName(detailList);
            return detailList;
        }
}
